<template>
  <div>
    <form-marker
      :isEdit="isEditPage" 
      :formData='list' 
      :rulesList='rules'
      :selectList="selectData"
      :eidtList="detailData"
      v-on:on-address="onchangeAddress"
      v-on:on-confirm="onSaveInfo" 
      v-on:on-cancel="onCancelInfo"></form-marker>
  </div>
</template>
<script>
import FormMarker from '@/components/createPage/createMarker'

export default {
  components: {
    FormMarker
  },
  data () {
    // 自定义验证规则
    let checkWarehouseCode = (rule, value, callback) => {
      if (value && this.pageType === 'new') {
        this.$http.post(`/${this.$const.PREFIX.MAINDATA}/mwarehouses/check`, {warehouseNo: value}).then((res) => {
          if (res.result) {
            if (!res.data) { // res.data.data [false]有重复/【true】无重复
              callback(new Error('编码已存在，请重新输入'))
            } else {
              callback()
            }
          } else {
            callback()
          }
        })
      } else {
        callback()
      }
    }
    // 验证address
    let onCheckAddress = (rule, value, callback) => {
      if (this.isEditPage || (this.checkAddress && this.checkAddress.provinceName && this.checkAddress.cityName && this.checkAddress.countyName)) {
        callback()
      } else {
        callback(new Error('省-市-县区-不允许为空'))
      }
    }
    return {
      list: [
        {
          type: 'input',
          label: '仓库编码',
          name: '仓库编码',
          prop: 'warehouseNo',
          isDisable: true
        },
        {
          type: 'input',
          label: '仓库名称',
          name: '仓库名称',
          prop: 'name'
        },
        {
          type: 'input',
          label: '助记码',
          name: '助记码',
          prop: 'mnemonic'
        },
        {
          type: 'radio',
          label: '仓库类型',
          prop: 'warehouseType',
          radioOptions: [
            {
              label: '1',
              name: '逻辑仓'
            },
            {
              label: '0',
              name: '物理仓'
            }
          ]
        },
        {
          type: 'radio',
          label: '是否退货仓',
          prop: 'isReturn',
          radioOptions: [
            {
              label: true,
              name: '是'
            },
            {
              label: false,
              name: '否'
            }
          ]
        },
        {
          type: 'radio',
          label: '是否良品仓',
          prop: 'isGood',
          radioOptions: [
            {
              label: true,
              name: '是'
            },
            {
              label: false,
              name: '否'
            }
          ]
        },
        {
          type: 'radio',
          label: '是否带有接口',
          prop: 'isWithInterface',
          radioOptions: [
            {
              label: true,
              name: '是'
            },
            {
              label: false,
              name: '否'
            }
          ]
        },
        {
          type: 'radio',
          label: '是否第三方仓库',
          prop: 'isThirdParty',
          radioOptions: [
            {
              label: true,
              name: '是'
            },
            {
              label: false,
              name: '否'
            }
          ]
        },
        {
          type: 'radio',
          label: '是否默认仓库',
          prop: 'isDefault',
          radioOptions: [
            {
              label: true,
              name: '是'
            },
            {
              label: false,
              name: '否'
            }
          ]
        },
        {
          type: 'select',
          label: '父级仓库',
          name: '父级仓库',
          prop: 'parentCode',
          propName: 'parentName',
          optionLabel: 'name',
          optionValue: 'code'
        },
        {
          type: 'selectProvince',
          label: '仓库地址',
          name: '仓库地址',
          prop: 'address',
          child: [
            {
              type: 'select',
              name: '省',
              prop: 'provinceName'
            },
            {
              type: 'select',
              name: '市',
              prop: 'cityName'
            },
            {
              type: 'select',
              name: '区县',
              prop: 'countyName'
            }
          ]
        },
        {
          type: 'input',
          name: '详细地址',
          prop: 'street'
        }
      ],
      rules: {
        warehouseNo: [
          { max: 32, message: '长度不可超过32个字符', trigger: 'change' },
          { required: true, message: '仓库编码不允许为空', trigger: 'change' },
          { validator: checkWarehouseCode, trigger: 'change' }
        ],
        mnemonic: [{ max: 32, message: '长度不可超过32个字符', trigger: 'change' }],
        warehouseType: [{ required: true, message: '请选择仓库类型', trigger: 'change' }],
        isReturn: [{ required: true, message: '请选择是否退货仓', trigger: 'change' }],
        // isGood: [{ required: true, message: '请选择是否良品仓', trigger: 'change' }],
        // isWithInterface: [{ required: true, message: '请选择是否带有接口', trigger: 'change' }],
        // isThirdParty: [{ required: true, message: '请选择是否第三方仓库', trigger: 'change' }],
        // isDefault: [{ required: true, message: '请选择是否默认仓库', trigger: 'change' }],
        name: [
          { max: 64, message: '长度不可超过64个字符', trigger: 'blur' },
          { required: true, message: '仓库名称不允许为空', trigger: 'blur' }
        ],
        address: [
          { required: true, validator: onCheckAddress, trigger: 'change' }
        ],
        street: [
          { required: true, message: '详细地址不允许为空', trigger: 'blur' },
          { max: 128, message: '长度不可超过128个字符', trigger: 'change' }
        ]
      },
      selectData: {},
      detailData: {},
      checkAddress: {},
      noRepeat: false,
      isEditPage: false
    }
  },
  mounted () {
    this.pageType = this.$route.params.id
    this.getWarehouseSelect()
    this.getWarehouseDetail()
  },
  methods: {
    // 获取父级仓库下拉列表数据
    async getWarehouseSelect () {
      let curId = this.pageType === 'new' ? null : this.pageType
      let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/mwarehouses/list`, {id: curId})
      this.selectData.parentCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取编辑详情数据
    async getWarehouseDetail () {
      if (this.pageType !== 'new') {
        let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/mwarehouses/getOne`, {id: this.pageType})
        this.detailData = data
        this.isEditPage = true
      }
    },
    onchangeAddress (val) {
      // console.log('onchangeAddress val', val)
      this.checkAddress = val
    },
    // 接口保存新建信息
    onSaveInfo (val) {
      let url = this.pageType === 'new' ? `/${this.$const.PREFIX.MAINDATA}/mwarehouses/add` : `/${this.$const.PREFIX.MAINDATA}/mwarehouses/update`
      let params = val
      params.isGood = val.isGood ? val.isGood : false
      params.isWithInterface = val.isWithInterface ? val.isWithInterface : false
      params.isThirdParty = val.isThirdParty ? val.isThirdParty : false
      params.isDefault = val.isDefault ? val.isDefault : false
      // params.isProcuringEntity = val.isProcuringEntity ? val.isProcuringEntity : false
      // params.shopHeaderAccountable = val.shopHeaderAccountable ? val.shopHeaderAccountable : false
      // params.isProcuringEntity = val.isProcuringEntity ? val.isProcuringEntity : false
      // params.shopHeaderAccountable = val.shopHeaderAccountable ? val.shopHeaderAccountable : false
      this.$http.post(url, params).then((res) => {
        if (res.result) {
          this.$message.success('操作成功')
          this.$router.push({name: 'warehouse'})
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    onCancelInfo () {
      this.$router.push({name: 'warehouse'})
    }
  }
}
</script>
<style lang="scss" scoped>

</style>


